//
//  HZL_LikeViewController.m
//  B_Project
//
//  Created by 胡佳俊 on 15/12/4.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_LikeViewController.h"
#import "HZL_LikeTableViewCell.h"
#import "HZL_StoreModel.h"
#import "HZL_FindModel.h"
#import "RESideMenu.h"
#import "HZL_StoreDetailViewController.h"
@interface HZL_LikeViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) CoreDataManager *manager;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *likeArr1;
@property (nonatomic, strong) NSArray *likeArr2;
@end

@implementation HZL_LikeViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    [self getData1];
    [self getData2];
    
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-liebiao"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    self.manager = [CoreDataManager shareInstance];
    [self getData1];
    [self getData2];
    [self createTableView];
    
}
- (void)getData1
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Store" inManagedObjectContext:self.manager.managedObjectContext];
    
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    
    //        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"businessId = %@", self.model.businessId];
    //        [fetchRequest setPredicate:predicate];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"%@", error);
    }
    self.likeArr1 = [NSArray arrayWithArray:fetchedObjects];

}
- (void)getData2
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Find" inManagedObjectContext:self.manager.managedObjectContext];
    
    [fetchRequest setEntity:entity];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"%@", error);
    }
    self.likeArr2 = [NSArray arrayWithArray:fetchedObjects];
    
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerClass:[HZL_LikeTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}
#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
#pragma mark - 分区高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
#pragma mark - 分区内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 30)];
        v1.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.5];
        UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
        l.text = @"精选收藏";
        [v1 addSubview:l];
        return v1;
    }
    if (section == 1) {
        UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 30)];
        v1.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.5];
        UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
        l.text = @"发现收藏";
        [v1 addSubview:l];
        return v1;
    }
    else{
        return nil;
    }
}
#pragma mark - 每个分区的cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.likeArr1.count;
    }
    else {
        return self.likeArr2.count;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HZL_StoreDetailViewController *sdVC = [[HZL_StoreDetailViewController alloc] init];
    if (indexPath.section == 0) {
        sdVC.model = self.likeArr1[indexPath.row];
    }else {
        sdVC.findModel = self.likeArr2[indexPath.row];
    }
    [self.navigationController pushViewController:sdVC animated:YES];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_LikeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.section == 0) {
        cell.model = self.likeArr1[indexPath.row];
    }
    if (indexPath.section == 1) {
        cell.findModel = self.likeArr2[indexPath.row];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
